/*
 * Endless Code License
 * Copyright (C) 2009 Jingul, Kim
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "ecGlyph.h"

#include "freetype/ftsynth.h"

JNIEXPORT jint JNICALL Java_ec_gfx_font_Glyph_getWidth(JNIEnv *, jobject,
		jint face) {
	return ((FT_Face)face)->glyph->bitmap.width;
}

JNIEXPORT jint JNICALL Java_ec_gfx_font_Glyph_getHeight(JNIEnv *, jobject,
		jint face) {
	return ((FT_Face)face)->glyph->bitmap.rows;
}

JNIEXPORT jint JNICALL Java_ec_gfx_font_Glyph_getTop(JNIEnv *, jobject,
		jint face) {
	return ((FT_Face)face)->glyph->bitmap_top;
}

JNIEXPORT jint JNICALL Java_ec_gfx_font_Glyph_getLeft(JNIEnv *, jobject,
		jint face) {
	return ((FT_Face)face)->glyph->bitmap_left;
}

JNIEXPORT jint JNICALL Java_ec_gfx_font_Glyph_getAdvX(JNIEnv *, jobject,
		jint face) {
	return ((FT_Face)face)->glyph->advance.x >> 6; // 1/64 픽셀
}

JNIEXPORT jint JNICALL Java_ec_gfx_font_Glyph_getAdvY(JNIEnv *, jobject,
		jint face) {
	return ((FT_Face)face)->glyph->advance.y >> 6;
}

JNIEXPORT void JNICALL Java_ec_gfx_font_Glyph_load(JNIEnv *env, jobject obj,
		jint aFace, jchar c) {
	FT_Face face = (FT_Face) aFace;

	FT_Error error;

	FT_UInt index = FT_Get_Char_Index(face, c);

	if (index == 0) {
		puts("no index");
	}

	error = FT_Load_Glyph(face, index, FT_LOAD_DEFAULT | FT_LOAD_NO_BITMAP);
}

JNIEXPORT jobject JNICALL Java_ec_gfx_font_Glyph_render(JNIEnv *env, jobject,
		jint aFace, jchar c) {
	FT_Face face = (FT_Face) aFace;

	FT_Error error;

	error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
	FT_GlyphSlot_Embolden(face->glyph);

	return env->NewDirectByteBuffer(face->glyph->bitmap.buffer,
			face->glyph->bitmap.width * face->glyph->bitmap.rows);
}
